package com.jbp.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.PoolGiftConfigDao;
import com.jbp.model.Gift;
import com.jbp.model.GiftLot;
import com.jbp.model.Pool;
import com.jbp.model.PoolGiftConfig;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;

import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PoolGiftConfigService extends ServiceImpl<PoolGiftConfigDao, PoolGiftConfig> implements IService<PoolGiftConfig> {

    @Autowired
    private PoolService poolService;
    @Autowired
    private GiftLotService giftLotService;
    @Autowired
    private GiftService giftService;

    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PoolGiftConfig> queryWrapper = new QueryWrapper<>();
        if (params.get("id") != null) {
            queryWrapper.like("id",params.get("id"));
        }
        if (params.get("giftId") != null) {
            queryWrapper.like("gift_id",params.get("giftId"));
        }
        if (params.get("giftNum") != null) {
            queryWrapper.like("gift_num",params.get("giftNum"));
        }
        if (params.get("poolId") != null) {
            queryWrapper.like("pool_id",params.get("poolId"));
        }
        if (params.get("updateTime") != null) {
            queryWrapper.like("update_time",params.get("updateTime"));
        }
        if (params.get("fromDate")!=null) {
            queryWrapper.ge("create_time", params.get("fromDate"));
        }
        if (params.get("toDate")!=null) {
            queryWrapper.le("DATE_FORMAT(create_time,   '%Y-%m-%d')", params.get("toDate"));
        }
        queryWrapper.orderByDesc("id");
        IPage<PoolGiftConfig> page = this.page(
            new Query<PoolGiftConfig>().getPage(params), queryWrapper
        );
        return new PageUtils(page);
    }


    public Map<Integer, Integer> getPoolGifts(Pool pool){
//        System.out.println("生成奖池");
        QueryWrapper<PoolGiftConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pool_id", pool.getId());
        List<PoolGiftConfig> list = list(queryWrapper);

        Map<Integer, Integer> map = new LinkedHashMap<>();
        for (PoolGiftConfig config : list) {
            if (config.getGiftNum()>0) {
                map.put(config.getGiftId(), config.getGiftNum());
            }
        }
        return map;
    }

    /**
     * 获取机器人奖池，抽中奖价值在500至6666钻
     * @param pool
     * @return
     */
    public Map<Integer, Integer> getRobPoolGifts(Pool pool){
        List<Gift> giftList = giftService.list();
        //key为giftid,value为coin(礼物价值)
        Map<Integer, Integer> mapCoin = new LinkedHashMap<>();
        if (giftList !=null && giftList.size() > 0){
            for (Gift gift : giftList) {
                mapCoin.put(gift.getId(), gift.getCoin());
            }
        }
        QueryWrapper<PoolGiftConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pool_id", pool.getId());
        List<PoolGiftConfig> list = list(queryWrapper);
        Map<Integer, Integer> map = new LinkedHashMap<>();
        for (PoolGiftConfig config : list) {
            int coin = 0;
            if (!mapCoin.isEmpty() && mapCoin.size() > 0){
                coin = mapCoin.get(config.getGiftId());//礼物价值
            }
            //抽中奖价值在500至6666钻
            if (config.getGiftNum()>0 && coin >= 500 && coin <= 6666) {
                map.put(config.getGiftId(), config.getGiftNum());
            }
        }
        return map;
    }


    //取礼物 从大到小排序
    public List<Gift> getGiftsByPoolId(Integer poolId){
        QueryWrapper<PoolGiftConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pool_id", poolId);
        List<PoolGiftConfig> list = list(queryWrapper);
        List<Gift> gifts = list.stream().map(config -> giftService.getById(config.getGiftId()))
                .sorted(Comparator.comparingInt(Gift::getCoin))
                .collect(Collectors.toList());
        return gifts;
    }

    /**生成gift配置*/
    public void geneData() {
        List<Pool> pools = poolService.list();
        for (Pool pool : pools) {
            List<GiftLot> giftLots = giftLotService.poolList(pool.getPrice() + "");
            for (GiftLot giftLot : giftLots) {
                // 存在就不加
                QueryWrapper<PoolGiftConfig> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("pool_id", pool.getId());
                queryWrapper.eq("gift_id", giftLot.getGiftId());
                if (getOne(queryWrapper) == null ) {
                    PoolGiftConfig config = new PoolGiftConfig();
                    config.setGiftId(giftLot.getGiftId());
                    config.setPoolId(pool.getId());
                    config.setGiftNum(0);
                    save(config);
//                    System.out.println("poolId:"+pool.getId()+" giftId:"+giftLot.getGiftId());
                }

            }
        }
    }
}